{include file="common/header"/}
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
      <div class="layui-col-md12">
        <div class="layui-card">
          <div class="layui-card-body">
            <div class="layui-row">
                <table class="layui-table" id="list" lay-filter="list"></table>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-group">
        <a href="javascript:;" class="layui-btn layui-btn-sm" onclick="xadmin.open('添加','{:url('add')}');"><i class="layui-icon layui-icon-add-circle"></i>{:lang('add')}栏目</a>
        <a href="javascript:;" class="layui-btn layui-btn-normal layui-btn-sm" onclick="openAll();"><i class="layui-icon layui-icon-reduce-circle"></i>展开或折叠全部</a>
    </div>
    
</script>


<script type="text/html" id="status">
    <input type="checkbox" name="ismenu" data-href="{:url('state')}" value="{{d.id}}" lay-skin="switch" lay-text="显示|隐藏" lay-filter="status" {{ d.ismenu == 1 ? 'checked' : '' }}>
</script>

<script type="text/html" id="icon">
    <span class="icon {{d.icon}}"></span>
</script>
<script type="text/html" id="action">
    <div class="layui-btn-group">
        <a class="layui-btn layui-btn-xs" onclick="xadmin.open('编辑','{:url('edit')}?id={{d.id}}')">{:lang('edit')}</a>
        <a class="layui-btn layui-btn-xs layui-btn-warm"  onclick="xadmin.open('添加','{:url('add')}?parentid={{d.id}}')">{:lang('add')}{:lang('child')}</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs"  data-href="{:url('del')}" lay-event="del">{:lang('del')}</a>
    </div>
</script>

{include file="common/footer"/}
<script>
    var tableIn=null, treeGrid=null,tableId='list';

    layui.extend({
        treeGrid: '/static/admin/xadmin/lib/treeGrid/treeGrid',
    }).use(['jquery','treeGrid','layer','form'], function(){
        var $=layui.jquery ,form = layui.form;
        treeGrid = layui.treeGrid;
        layer=layui.layer;
        tableIn=treeGrid.render({
            id:tableId
            ,elem: '#'+tableId
            ,idField:'id'
            ,url:'{:url("index")}'
            ,toolbar: '#toolbarDemo'
            ,cellMinWidth: 100
            ,treeId:'id'//树形id字段名称
            ,treeUpId:'pid'//树形父id字段名称
            ,treeShowName:'title'//以树形式显示的字段
            ,height:'full-140'
            ,isFilter:false
            ,iconOpen:true//是否显示图标【默认显示】
            // ,isOpenDefault:true//节点默认是展开还是折叠【默认展开】
            ,cols: [[
                {field: 'id', title: '{:lang("id")}', width: 70, fixed: true},
                {field: 'title', title: '栏目名称', width: 200},
                {field: 'catdir', title: '栏目目录', width: 200},
                {field: 'seo_title', title: 'SEO标题', minwidth: 200},
                {field: 'total', title: '数据', minwidth: 200},
                {field: 'ismenu',align: 'center',title: '菜单{:lang("status")}', width: 150,toolbar: '#status'},
                {title:'操作',width:200, toolbar: '#action',align:"center"},
            ]]
            ,page:false
        });
        treeGrid.on('tool(list)', function(obj){
            var data = obj.data;
            var url = $(this).attr('data-href');
            if(obj.event === 'del'){
                layer.confirm('确定要删除吗？', function(index){
                    loading =layer.load(1, {shade: [0.1,'#fff']});
                    $.post(url?url:'delete',{ids:[data.id]},function(res){
                        layer.close(loading);
                        layer.close(index);
                        if(res.code>0){
                            layer.msg(res.msg,{time:1000,icon:1});
                            obj.del();
                        }else{
                            layer.msg(res.msg,{time:1000,icon:2});
                        }
                    });
                });
            }

        });

        form.on('switch(status)', function(obj){
            var name = obj.elem.getAttribute('name');
            var url = obj.elem.getAttribute('data-href');;
            var id = this.value;
            loading =layer.load(1, {shade: [0.1,'#fff']});
            $.post(url,{'id':id,'field':name},function (res) {
                layer.close(loading);
                if (res.code==1) {
                    layer.msg(res.msg,{time:1000,icon:1},function(){
                        location.href = res.url;
                    });
                    return false;
                }else{
                    layer.msg(res.msg,{time:1000,icon:2});
                    return false;
                }
            })
        }); 
        $('body').on('blur','.list_order',function() {
           var id = $(this).attr('data-id');
           var sort = $(this).val();
           $.post('{:url("order")}',{id:id,sort:sort},function(res){
                if(res.code==1){
                    layer.msg(res.msg,{time:1000,icon:1},function(){
                        location.href = res.url;
                    });
                }else{
                    layer.msg(res.msg,{time:1000,icon:2});
                    window.location.reload();
                }
           })
        })
    });
    function openAll() {
        var treedata=treeGrid.getDataTreeList(tableId);
        treeGrid.treeOpenAll(tableId,!treedata[0][treeGrid.config.cols.isOpen]);
    }

</script>